package com.puyo;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class OptionPane extends JFrame implements ActionListener {

	private static final long serialVersionUID = 1L;

	private JPanel controlsPane;
	private JPanel optionBanner;
	private JButton moveLeft, moveRight, moveDown, rotate, reset, menu;
	private JLabel moveLeftl, moveRightl, moveDownl, rotatel;
	private int setControl = 0;
	private MenuPane mp;

	/**
	 * @param mp
	 * Constructor for optionPane
	 */
	public OptionPane(MenuPane mp) {
		super();
		this.mp = mp;
		Container optionPane = getContentPane();
		controlsPane = new JPanel();
		optionBanner = new JPanel();

		optionPane.setLayout(new BorderLayout());
		GridLayout gd = new GridLayout(6, 2);
		gd.setVgap(10);
		gd.setHgap(10);
		controlsPane.setLayout(gd);

		moveLeft = new JButton("Move Left");
		moveRight = new JButton("Move Right");
		moveDown = new JButton("Move Down");
		rotate = new JButton("Rotate");
		reset = new JButton("Reset Defaults");
		menu = new JButton("Menu");

		moveLeft.addActionListener(this);
		moveRight.addActionListener(this);
		moveDown.addActionListener(this);
		rotate.addActionListener(this);
		reset.addActionListener(this);
		menu.addActionListener(this);

		moveLeftl = new JLabel(KeyEvent.getKeyText(DefineControls.KEY_LEFT));
		moveRightl = new JLabel(KeyEvent.getKeyText(DefineControls.KEY_RIGHT));
		moveDownl = new JLabel(KeyEvent.getKeyText(DefineControls.KEY_DOWN));
		rotatel = new JLabel(KeyEvent.getKeyText(DefineControls.KEY_UP));
		JLabel ks = new JLabel(" Key Settings:");
		ks.setFont(new Font("Arial", Font.BOLD, 20));
		controlsPane.add(ks);
		controlsPane.add(new Label(""));
		controlsPane.add(moveLeft);
		controlsPane.add(moveLeftl);
		controlsPane.add(moveRight);
		controlsPane.add(moveRightl);
		controlsPane.add(moveDown);
		controlsPane.add(moveDownl);
		controlsPane.add(rotate);
		controlsPane.add(rotatel);
		controlsPane.add(reset);
		controlsPane.add(menu);

		JLabel img1 = new JLabel("");
		img1.setIcon(new ImageIcon("optionBanner.png"));
		optionBanner.add(img1);
		optionPane.add(optionBanner, BorderLayout.NORTH);
		optionPane.add(controlsPane, BorderLayout.CENTER);
		setBounds(300, 50, 300, 438);
		setVisible(false);
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				switch(setControl){
				case 1:
					DefineControls.KEY_LEFT = e.getKeyCode();
					break;
				case 2:
					DefineControls.KEY_RIGHT = e.getKeyCode();
					break;
				case 3:
					DefineControls.KEY_DOWN = e.getKeyCode();
					break;
				case 4:
					DefineControls.KEY_UP = e.getKeyCode();
					break;
				}
				setLabels();
				setControl = 0;
			}
		});
		setFocusable(true);
	}

	/**
	 * Sets the labels for the options
	 */
	public void setLabels() {
		moveLeftl.setText(KeyEvent.getKeyText(DefineControls.KEY_LEFT));
		moveRightl.setText(KeyEvent.getKeyText(DefineControls.KEY_RIGHT));
		moveDownl.setText(KeyEvent.getKeyText(DefineControls.KEY_DOWN));
		rotatel.setText(KeyEvent.getKeyText(DefineControls.KEY_UP));
	}

	/**
	 * Resets to default key configuration
	 */
	public void resetDefaults() {
		DefineControls.KEY_LEFT = KeyEvent.VK_LEFT;
		DefineControls.KEY_RIGHT = KeyEvent.VK_RIGHT;
		DefineControls.KEY_DOWN = KeyEvent.VK_DOWN;
		DefineControls.KEY_UP = KeyEvent.VK_UP;
		setLabels();
	}

	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 *
	 */
	public void actionPerformed(ActionEvent e) {
		String message ="Press a key";
		if (e.getSource() == menu) {
			mp.setVisible(true);
			setVisible(false);
		} else if (e.getSource() == moveLeft) {
			setLabels();
			setControl = 1;
			moveLeftl.setText(message);
		} else if (e.getSource() == moveRight) {
			setLabels();
			setControl = 2;
			moveRightl.setText(message);
		} else if (e.getSource() == moveDown) {
			setLabels();
			setControl = 3;
			moveDownl.setText(message);
		} else if (e.getSource() == rotate) {
			setLabels();
			setControl = 4;
			rotatel.setText(message);
		} else if (e.getSource() == reset) {
			resetDefaults();
		}
		menu.transferFocus();
	}
}
